//
// Created by denglibin on 19-10-29.
//
/**
 *
 * 本地套接字客户端
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <ctype.h>
#include <strings.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <stddef.h>
#include "wrap.h"
#define SERV_ADDR "foo.socket"
#define CLIENT_ADDR "client.socket"

static int main_(void){

    int cfd, len;
    struct sockaddr_un server_addr, client_addr;
    char buf[4096];

    cfd = Socket(AF_UNIX, SOCK_STREAM, 0);

    bzero(&client_addr, sizeof(client_addr));
    client_addr.sun_family = AF_UNIX;
    strcpy(client_addr.sun_path, CLIENT_ADDR);

    len = offsetof(struct sockaddr_un, sun_path) + strlen(client_addr.sun_path);
    unlink(CLIENT_ADDR);
    Bind(cfd, (struct sockaddr*)&client_addr, len);

    /*构造server地址*/
    bzero(&server_addr, sizeof(server_addr));
    server_addr.sun_family = AF_UNIX;
    strcpy(server_addr.sun_path, SERV_ADDR);

    len = offsetof(struct sockaddr_un, sun_path) + strlen(server_addr.sun_path);

    connect(cfd, (struct sockaddr *) &server_addr, len);

    while(fgets(buf, sizeof(buf), stdin) != NULL){
        write(cfd, buf, strlen(buf));
        len = read(cfd, buf, sizeof(buf));
        write(STDOUT_FILENO, buf, len);
    }
    close(cfd);


    return 0;
}
/*
CC=gcc
all: server client

server: local_socket_server.c wrap.o
	${CC} local_socket_server.c wrap.o -o server
client: local_socket_client.c wrap.o
	${CC} local_socket_client.c wrap.o -o client
wrap.o: wrap.c
	${CC} -c wrap.c
clean:
	rm *.o
*/